/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import entity.Message;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import session.MessageFacade;

/**
 *
 * @author owtee
 */
public class GetMessage extends HttpServlet {
    @EJB
    private MessageFacade messageFacade;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/xml;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            
            String mesId = request.getParameter("mesId");
            
            Message mes = messageFacade.find(mesId);
            
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
            //creating a new instance of a DOM to build a DOM tree.
            Document doc = docBuilder.newDocument();
            //new XmlServlet().createXmlTree(doc);

            Element msg = doc.createElement("Messenger");
            doc.appendChild(msg);
            // Create element company
            Element tagMessage = doc.createElement("Message");
            msg.appendChild(tagMessage);
            Element tagMesId = doc.createElement("MesId");
            tagMessage.appendChild(tagMesId);
            Text txtMesId = doc.createTextNode(mes.getMesId());
            tagMesId.appendChild(txtMesId);
            Element tagMesType = doc.createElement("MesType");
            tagMessage.appendChild(tagMesType);
            Text txtMesType = doc.createTextNode(mes.getMesType());
            tagMesType.appendChild(txtMesType);
            Element tagMesDetail = doc.createElement("MesDetail");
            tagMessage.appendChild(tagMesDetail);
            Text txtMesDetail = doc.createTextNode(mes.getMesDetail());
            tagMesDetail.appendChild(txtMesDetail);                           

            //TransformerFactory instance is used to create Transformer objects.
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();

            transformer.setOutputProperty(OutputKeys.INDENT, "yes");

            // create string from xml tree
            StringWriter sw = new StringWriter();
            StreamResult result = new StreamResult(sw);
            DOMSource source = new DOMSource(doc);
            transformer.transform(source, result);
            String xmlString = sw.toString().replace("&#13;", "");                                  

            out.println(xmlString);
            
        } catch(Exception ex) {
            out.println("Error : " + ex.getMessage());
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
